#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <json-c/json.h>

char *json_str = "{ \
'result': {\
    'msg': 'ok', \
    'rlt': 'True', \
    'coursewares': [ \
        'http://10.1.250.71:8080/ICSmananger/upload/coursewaresource/11401339779640/lecture.ics', \
        'http://10.1.250.71:8080/ICSmananger/upload/videos/5938d678-facb-45b9-9d40-700368f472e3.mp4', \
        'http://10.1.250.71:8080/ICSmananger/upload/videos/5938d678-facb-45b9-9d40-700368f472e3.jpg', \
        'http://10.1.250.71:8080/ICSmananger/upload/videos/85de8341-4d70-4230-8923-0e36fded2414.mp4', \
        'http://10.1.250.71:8080/ICSmananger/upload/videos/85de8341-4d70-4230-8923-0e36fded2414.jpg', \
        'http://10.1.250.71:8080/ICSmananger/upload/videos/9d0a0ab3-2310-436d-9061-56656f63e0b6.mp4', \
        'http://10.1.250.71:8080/ICSmananger/upload/videos/9d0a0ab3-2310-436d-9061-56656f63e0b6.jpg' \
        ], \
    'questions': [ \
        '07fea5cdead4426597e75f4b2d086bbb', \
        '3a7be5aa75eb415e8c1587085a4c6e5d', \
        '4d04a01e598749d6bec8eb3846db79c1', \
        '5e79aae3fe704e16b0193ca793123599' \
    ]\
}}";


int main(int argc, char *argv[])
{
    json_object *pobj = NULL;
    json_object *result, *questions, *coursewares, *o;
    int i, len;

    pobj = json_tokener_parse(json_str);
    if (pobj == NULL) {
        fprintf(stderr, "parse json string error!\n");
        exit(1);
    }

    printf("new_obj.to_string()=%s\n", json_object_to_json_string(pobj));

    /* get root element */
    if (json_object_object_get_ex(pobj, "result", &result) == FALSE) {
        fprintf(stderr, "json obj 'result' not found!\n");
        exit(1);
    }

    /* get questions element */
    if (json_object_object_get_ex(result, "questions", &questions) == FALSE) {
        fprintf(stderr, "json obj 'questions' not found!\n");
        exit(1);
    }

    /* check the type of element */
    if (json_object_get_type(questions) != json_type_array) {
        fprintf(stderr, "json type 'questions' error!\n");
        exit(1);
    }

    /* output result.questions */
    len = json_object_array_length(questions);
    fprintf(stderr, "result.questions[%d]=\n", len);
    for (i = 0; i < len; i++) {
        o = json_object_array_get_idx(questions, i);
        printf("\t[%d]=%s\n", i, json_object_to_json_string(o));
    }

    /* get coursewares element */
    if (json_object_object_get_ex(result, "coursewares", &coursewares) == FALSE) {
        fprintf(stderr, "json obj 'coursewares' not found!\n");
        exit(1);
    }

    /* check the type of element */
    if (json_object_get_type(coursewares) != json_type_array) {
        fprintf(stderr, "json type 'coursewares' error!\n");
        exit(1);
    }

    /* output result.coursewares */
    len = json_object_array_length(coursewares);
    fprintf(stderr, "result.coursewares[%d]=\n", len);
    for (i = 0; i < len; i++) {
        o = json_object_array_get_idx(coursewares, i);
        printf("\t[%d]=%s\n", i, json_object_to_json_string(o));
    }

    json_object_put(pobj);

    exit(0);
}

